#include "datapanel.h"
#include "ui_datapanel.h"
#include "iconfont.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
DataPanel::DataPanel(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::DataPanel),
    m_font_color("#000000")
{
    ui->setupUi(this);
    setPlugsToView(parent);
}

DataPanel::~DataPanel()
{
    delete ui;
}

void DataPanel::setBasicTemplate(QString bac_color, QString shadow_color, QString titel, QChar icon, QString unit,QString color)
{
    m_font_color = color;
    setStyle(bac_color,shadow_color);
    setTitel(titel,m_font_color);
    setIcon(icon,m_font_color);
    setUnit(unit,m_font_color);
}

void DataPanel::setStyle(QString bac_color,QString shadow_color,qint64 radius)
{
    //设置具体阴影
    auto shadow_effect = new QGraphicsDropShadowEffect;
    shadow_effect->setOffset(0, 0);
    //阴影颜色
    shadow_effect->setColor(QColor(shadow_color));
    //阴影半径
    shadow_effect->setBlurRadius(radius);
    // 控件设置
    this->setGraphicsEffect(shadow_effect);
    QString style = "QFrame#%1{border:2px solid %2;border-radius:%3px;background-color: %4;}";
    this->setStyleSheet(style.arg(this->objectName()).arg(bac_color).arg(radius).arg(bac_color));
}

void DataPanel::setTitel(QString titel, QString color)
{
    ui->lbl_titel->setText(titel);
    QString style = "color:%1";
    ui->lbl_titel->setStyleSheet(style.arg(color));
}

void DataPanel::setIcon(QChar icon, QString color)
{
    ui->lbl_icon->setFont(IconFont::IconFontInstance()->iconFont);
    ui->lbl_icon->setText(icon);
    QString style = "color:%1;font-size:26px;";
    ui->lbl_icon->setStyleSheet(style.arg(color));
}

void DataPanel::setValue(QString value, bool state)
{
    ui->lbl_data->setText(value);
    QString style = "color:%1";
    ui->lbl_data->setStyleSheet(style.arg(m_font_color));

    if(state){
        setStyle("#fef0f0","#f56c6c");
        return;
    }
    setStyle("#ffffff","#c0c0c0");
}

void DataPanel::setValue(QString value,int threshold,emComparisonCondition em_CCondition)
{
    ui->lbl_data->setText(value);
    QString style = "color:%1";
    ui->lbl_data->setStyleSheet(style.arg(m_font_color));

    switch (em_CCondition) {
    case LessThan://目标值小于阈值
        if(value.toInt() < threshold){
            qDebug() << value << "小于" << threshold;
            setStyle("#fef0f0","#f56c6c");
        }
        else{
            setStyle("#ffffff","#c0c0c0");
        }
    case GreaterThan://目标值大于阈值
        if(value.toInt() > threshold){
            setStyle("#fef0f0","#f56c6c");
            qDebug() << value << "大于" << threshold;
        }
        else{
            setStyle("#ffffff","#c0c0c0");
        }
    case EqualTo://目标值等于阈值
        if(value.toInt() == threshold) setStyle("#fef0f0","#f56c6c");
        else setStyle("#ffffff","#c0c0c0");
//        setStyle("#ffffff","#c0c0c0");
    }
}

void DataPanel::setUnit(QString unit, QString color)
{
    ui->lbl_uint->setText(unit);
    QString style = "color:%1";
    ui->lbl_uint->setStyleSheet(style.arg(color));
}

void DataPanel::setPlugsToView(QWidget *view)
{
    QVBoxLayout *layout = new QVBoxLayout(view);
    layout->setContentsMargins(2,2,2,2);
    layout->addWidget(this);
    view->setLayout(layout);
}
